import Vue from 'vue'
import VueRouter from 'vue-router'

//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Manager',
    component: () => import('@/views/Manager.vue'),
    redirect: '/home',
    meta: {
      title: '后台管理'
    },
    children: [
      {
        path: '/home',
        name: 'Home',
        meta: {
          title: '系统首页',
          icon: 'el-icon-house'
        },
        component: () => import('@/views/manager/Home.vue')
      },
      // {
      //   path: '/info/admin',
      //   name: 'Admin',
      //   meta: {
      //     title: '管理员管理',
      //     parent: {
      //       title: '信息管理',
      //       icon: 'el-icon-menu'
      //     },
      //     showInbreadcrumb: true
      //   },
      //   component: () => import('@/views/manager/info/Admin.vue')
      // },
      {
        path: '/info/user',
        name: 'User',
        meta: {
          title: '用户管理',
          parent: {
            title: '信息管理',
            icon: 'el-icon-menu'
          },
          showInbreadcrumb: true
        },
        component: () => import('@/views/manager/info/user/User.vue')
      },
      {
        path: '/info/goods',
        name: 'Goods',
        meta: {
          title: '产物管理',
          parent: {
            title: '信息管理',
            icon: 'el-icon-menu'
          },
          showInbreadcrumb: true
        },
        component: () => import('@/views/manager/info/Goods.vue')
      },
      {
        path: '/info/goodsType',
        name: 'GoodsType',
        meta: {
          title: '产物分类管理',
          parent: {
            title: '信息管理',
            icon: 'el-icon-menu'
          },
          showInbreadcrumb: true
        },
        component: () => import('@/views/manager/info/GoodsType.vue')
      },
      {
        path: '/info/storage',
        name: 'Storage',
        meta: {
          title: '仓库管理',
          parent: {
            title: '信息管理',
            icon: 'el-icon-menu'
          },
          showInbreadcrumb: true
        },
        component: () => import('@/views/manager/info/Storage.vue')
      },
      {
        path: '/info/source',
        name: 'Source',
        meta: {
          title: '产地管理',
          parent: {
            title: '信息管理',
            icon: 'el-icon-menu'
          },
          showInbreadcrumb: true
        },
        component: () => import('@/views/manager/info/Source.vue')
      },
      {
        path: '/info/record',
        name: 'Record',
        meta: {
          title: '记录管理',
          parent: {
            title: '信息管理',
            icon: 'el-icon-menu'
          },
          showInbreadcrumb: true
        },
        component: () => import('@/views/manager/info/Record.vue')
      },
      {
        path: '/file',
        name: 'File',
        meta: {
          title: '文件管理',
          icon: 'el-icon-document',
          showInbreadcrumb: true
        },
        component: () => import('@/views/manager/File.vue')
      },
      {
        path: '/person',
        name: 'Person',
        meta: {
          title: '个人信息',
          icon: 'el-icon-person',
          showInbreadcrumb: true
        },
        component: () => import('@/views/manager/Person.vue')
      }

      // {
      //   path: '/test',
      //   name: 'test',
      //   meta: {
      //     title: '首页',
      //     showInbreadcrumb: true
      //   },
      //   component: () => import('@/views/manager/Test.vue')
      // }
    ]
  },
  {
    path: '/front',
    name: 'Front',
    component: () => import('@/views/front/Front.vue'),
    redirect: '/front/home',
    children: [
      {
        path: '/front/home',
        name: 'FrontHome',
        component: () => import('@/views/front/Home.vue')
      },
      {
        path: '/front/goodsList',
        name: 'FrontGoodsList',
        component: () => import('@/views/front/GoodsList.vue')
      },
      {
        path: '/front/goodsDetail/:id',
        name: 'FrontGoodsDetail',
        props: true,
        component: () => import('@/views/front/GoodsDetail.vue')
      },
      {
        path: '/front/types',
        name: 'FrontTypes',
        component: () => import('@/views/front/Types.vue')
      },
      {
        path: '/front/source',
        name: 'FrontSource',
        component: () => import('@/views/front/Source.vue')
      },
      {
        path: '/front/person',
        name: 'FrontPerson',
        component: () => import('@/views/front/Person.vue')
      },
      {
        path: '/front/password',
        name: 'FrontPassword',
        component: () => import('@/views/front/Password.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/Register.vue')
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 提供一个重置路由的方法
export const resetRouter = () => {
  router.matcher = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
  })
}

router.beforeEach((to, from, next) => {
  // console.log(to.matched)
  // 未找到路由的情况
  if (!to.matched.length) {
    next('/404')
  }
  // 是否访问管理端
  if (to.matched[0].meta.title === '后台管理') {
    // 是否登录
    if (localStorage.getItem('curUser')) {
      // 如果用户角色是普通用户，则进入客户端首页
      if (JSON.parse(localStorage.getItem('curUser')).roleId == 1) {
        next('/front')
      }
    } else {
      // 跳回登录页面
      next('/login')
    }
  }
  // 其他情况就放行
  next()
})

export default router
